<?php
class WPaymentAuthorizenetVoid extends USystemWorklet
{
	public function run($order)
	{
		include_once(dirname(__FILE__).'/../components/anet_php_sdk/AuthorizeNet.php');
		
		defined("AUTHORIZENET_API_LOGIN_ID")
			|| define("AUTHORIZENET_API_LOGIN_ID", $this->param('apiLogin'));
    	defined("AUTHORIZENET_TRANSACTION_KEY")
			|| define("AUTHORIZENET_TRANSACTION_KEY", $this->param('apiTransactionKey'));
    	defined("AUTHORIZENET_SANDBOX")
			|| define("AUTHORIZENET_SANDBOX", ($this->param('sandbox') == '1'));
    	
    	$id = explode(':',$order->custom);
		$id = count($id)>1?$id[1]:$id[0];
    	
    	$void = new AuthorizeNetAIM;
        $void_response = $void->void($id);
		
		if($void_response->approved)
		{
			wm()->get('payment.order')->void($order->id);
			return true;
		}
		return false;
	}
}